/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rowan.infovis.soccer;

import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author Kevin McGarry
 */
public class TeamManager {

    private ArrayList<Team> teams = new ArrayList<Team>();
    private ArrayList<String> teamNames = new ArrayList<String>();
    private Team currentTeam;
    private Team secondCurrentTeam;

    public void setTeam( String name ) {
        if( !teamNames.contains(name) ) {
            addTeam(name);
        }

        for(int i=0; i<teams.size(); i++) {
            if (name.compareTo(teams.get(i).getName()) == 0) {
                currentTeam = teams.get(i);
                break;
            }
        }
    }

    public void setSecondTeam( String name ) {
        if( !teamNames.contains(name) ) {
            addTeam(name);
        }

        for(int i=0; i<teams.size(); i++) {
            if (name.compareTo(teams.get(i).getName()) == 0) {
                secondCurrentTeam = teams.get(i);
                break;
            }
        }
    }

    public void sortTeams() {
        Collections.sort(teamNames);
        for(int i=0; i<teams.size(); i++){
            teams.get(i).sortPlayers();
        }
        for(int i=0; i<teams.size(); i++){
            if(teamNames.get(0).compareTo(teams.get(i).getName()) == 0) {
                currentTeam = teams.get(i);
                secondCurrentTeam = teams.get(i);
            }
        }

    }

    private void addTeam(String name) {
        teamNames.add(name);
        
        Team newTeam = new Team(name);
        teams.add(newTeam);
    }

    public Team getCurrentTeam() {
        return currentTeam;
    }

    public Team getSecondCurrentTeam() {
        return secondCurrentTeam;
    }

    public ArrayList<String> getTeamNames(String letter) {
        ArrayList<String> teamNameByLetter = new ArrayList<String>();
        for(int i=0; i<teamNames.size(); i++) {
            String firstLetter = teamNames.get(i).substring(0,1);
            if(letter.compareTo(firstLetter)==0) {
                teamNameByLetter.add(teamNames.get(i));
            }
        }
        return teamNameByLetter;
    }
}
